package mo.offer_special;

public class L001 {

    public int divide(int a, int b) {
        if (a == 0) {
            return 0;
        }
        int MIN = Integer.MIN_VALUE, MAX = Integer.MAX_VALUE, MIN_LIMIT = MIN >> 1;
        if (a == MIN && b == -1) {
            return MAX;
        }
        boolean isPos = (a >= 0 || b <= 0) && (a <= 0 || b >= 0);
        if (a > 0) {
            a = -a;
        }
        if (b > 0) {
            b = -b;
        }
        int ans = 0;
        while (a <= b) {
            int d = b, c = 1;
            while (d >= MIN_LIMIT && d + d >= a) {
                d += d;
                c += c;
            }
            a -= d;
            ans += c;
        }
        return isPos ? ans : -ans;
    }

}
